package com.company.project.common;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

//演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {

	 /**
	  * 读取控制台内容
	  */
	 public static String scanner(String tip) {
	     @SuppressWarnings("resource")
		 Scanner scanner = new Scanner(System.in);
	    
	     StringBuilder help = new StringBuilder();
	     help.append("请输入" + tip + "：");
	     System.out.println(help.toString());
	     if (scanner.hasNext()) {
	         String ipt = scanner.next();
	         if (StringUtils.isNotEmpty(ipt)) {
	             return ipt;
	         }
	     }
	     throw new MybatisPlusException("请输入正确的" + tip + "！");
	 }
	
	 public static void main(String[] args) {
	     // 代码生成器
	     AutoGenerator mpg = new AutoGenerator();
	
	     // 全局配置
	     GlobalConfig gc = new GlobalConfig();
	     gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
	     gc.setAuthor("任建波");
	     gc.setOpen(false);
	     mpg.setGlobalConfig(gc);
	
	     // 数据源配置
	     DataSourceConfig dsc = new DataSourceConfig();
	     dsc.setUrl("jdbc:mysql://47.104.168.163:3306/yimengxiangxian?useUnicode=true&useSSL=false&characterEncoding=utf8");
	     dsc.setDriverName("com.mysql.jdbc.Driver");
	     dsc.setUsername("root");
	     dsc.setPassword("roottengzhikejiA");
	     mpg.setDataSource(dsc);
	
	     // 包配置
	     PackageConfig pc = new PackageConfig();
	     pc.setModuleName(scanner("模块名"));
	     pc.setParent("com.company.project");
	     mpg.setPackageInfo(pc);
	
	     // 策略配置
	     StrategyConfig strategy = new StrategyConfig();
	     strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
	     strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
//	     strategy.setInclude(scanner("user,payorder").split(","));
	     strategy.setInclude(scanner("config_file"));
	     strategy.setControllerMappingHyphenStyle(true);//驼峰转连字符
	     strategy.setTablePrefix(pc.getModuleName() + "_");//表前缀
	     mpg.setStrategy(strategy);
	     
	     mpg.execute();
	 }

}